#if USE_HybridCLR



using HybridCLR.Editor;
using HybridCLR.Editor.Settings;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

/*
*Creater:Hyx
*Introduce:介绍
*Version:0.0.1
*ChangeLog:创建--2024.01.01
*/


public class HybridTools
{


    [MenuItem("HybridCLR/Tools/0. 编译 + copy目标平台热更脚本", false, 301)]
    public static void All()
    {
        HybridCLR.Editor.Commands.CompileDllCommand.CompileDll(EditorUserBuildSettings.activeBuildTarget);

        Debug.Log($"Compile Dll Active Build Target Copy Finished!");

        CopyDllToAssets();

    }

    [MenuItem("HybridCLR/Tools/1. 编译目标平台热更脚本", false, 301)]
    public static void CompileDllActiveBuildTargetCopy()
    {
        HybridCLR.Editor.Commands.CompileDllCommand.CompileDll(EditorUserBuildSettings.activeBuildTarget);

        Debug.Log($"Compile Dll Active Build Target Copy Finished!");

    }

    // 复制热更的DLL到资源目录，以备用AB包导出
    [MenuItem("HybridCLR/Tools/2. 复制热更的DLL到资源目录", false, 301)]
    public static void CopyDllToAssets()
    {
        BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
        string buildDir = SettingsUtil.GetHotUpdateDllsOutputDirByTarget(target);
        // 项目配置的热更dll
        for (int i = 0; i < HybridCLRSettings.Instance.hotUpdateAssemblyDefinitions.Length; i++)
        {
            string fileName = HybridCLRSettings.Instance.hotUpdateAssemblyDefinitions[i].name + ".dll";
            string sourcePath = Directory.GetFiles(buildDir).ToList().Find(hotPath => hotPath.Contains(fileName));
            if (string.IsNullOrEmpty(sourcePath))
            {
                Debug.Log($"热更程序集不存在: {buildDir} / {fileName}");
                Debug.LogError($"热更程序集不存在: {buildDir} / {fileName}");
                continue;
            }

            // 将程序集添加后缀 .bytes 并复制到AB包路径下
            string newFileName = fileName + ".bytes";
            //todo... 你的工程：目标目录路径 //Assets/Res/AOTAssembly/HotUpdate.dll.bytes
            string targetDirectory = Application.dataPath + "/HybridCLR/AOTAssembly";

            Debug.Log($"目标目录路径:{targetDirectory}/{newFileName} ");
            // 检查源文件是否存在
            if (File.Exists(sourcePath))
            {
                // 构建目标文件的完整路径
                string destinationPath = Path.Combine(targetDirectory, newFileName);
                // 检查目标目录是否存在，如果不存在则创建
                if (!Directory.Exists(targetDirectory))
                {
                    Directory.CreateDirectory(targetDirectory);
                }

                // 如果目标文件已经存在，则删除
                if (File.Exists(destinationPath))
                {
                    File.Delete(destinationPath);
                }

                // 将源文件复制到目标目录下，并修改名称
                File.Copy(sourcePath, destinationPath);
                // 刷新资源，使其在 Unity 编辑器中可见
                AssetDatabase.Refresh();
                Debug.Log("File copied successfully!");
            }
            else
            {
                Debug.LogError("Source file does not exist!");
            }
        }

        Debug.Log("复制热更的DLL到资源目录 完成!!!");
    }


    /// <summary>
    /// 
    /// </summary>
    [MenuItem("HybridCLR/Tools/3. 复制补充元数据的DLL到资源目录", false, 301)]
    public static void CopyAOTAssembliesToStreamingAssets()
    {
        var target = EditorUserBuildSettings.activeBuildTarget;
        string aotAssembliesSrcDir = SettingsUtil.GetAssembliesPostIl2CppStripDir(target);
        string aotAssembliesDstDir = Application.streamingAssetsPath;

        foreach (var dll in SettingsUtil.AOTAssemblyNames)
        {
            string srcDllPath = $"{aotAssembliesSrcDir}/{dll}.dll";
            if (!File.Exists(srcDllPath))
            {
                Debug.LogError($"ab中添加AOT补充元数据dll:{srcDllPath} 时发生错误,文件不存在。裁剪后的AOT dll在BuildPlayer时才能生成，因此需要你先构建一次游戏App后再打包。");
                continue;
            }
            string dllBytesPath = $"{aotAssembliesDstDir}/{dll}.dll.bytes";
            File.Copy(srcDllPath, dllBytesPath, true);
            Debug.Log($"[CopyAOTAssembliesToStreamingAssets] copy AOT dll {srcDllPath} -> {dllBytesPath}");
        }
        AssetDatabase.Refresh();

    }
}

#endif